package com.zhj.micro.product.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import com.zhj.micro.product.entity.ProductStorage;
import com.zhj.micro.product.entity.dto.ProductDTO;
import com.zhj.micro.product.mapper.ProductMapper;
import com.zhj.micro.product.entity.Product;
import com.zhj.micro.product.mapper.ProductStorageMapper;
import com.zhj.micro.product.service.ProductService;


@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductStorageMapper productStorageMapper;

    @Override
    @Transactional
    public int insert(ProductDTO productDTO) {
        Product product = new Product();
        BeanUtils.copyProperties(productDTO, product);
        //添加商品到库中
        int insert = productMapper.insert(product);
        if (insert > 0) {
            //商品添加成功后，设置库存
            ProductStorage productStorage = new ProductStorage();
            productStorage.setProductId(product.getProductId());
            productStorage.setProductStorageFreeze(0L);
            productStorage.setProductStorage(productDTO.getStorageNum());
            this.productStorageMapper.insert(productStorage);
        }
        return insert;
    }

}
